﻿using Groove.Core.Player;
using SchwabenCode.QuickIO;
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;

namespace Groove.Core.MediaFile
{
    public class MediaFile : IMediaFile,INotifyPropertyChanged
    {
        private const string UnknownString = "<Unknown>";

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 文件完整路径
        /// </summary>
        public string FullFileName { get; }

        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; private set; }

        /// <summary>
        /// 专辑
        /// </summary>
        public string Album { get; private set; }

        /// <summary>
        /// 艺术家
        /// </summary>
        public string Artists { get; private set; }
           
        /// <summary>
        /// 风格流派
        /// </summary>
        public string Genres { get; private set; }

        /// <summary>
        /// 专辑封面
        /// </summary>
        public Bitmap AlbumCover => CoverManager.GetImageFromFile(FullFileName);

        private bool _isPlaying;
        /// <summary>
        /// 在播放
        /// </summary>
        public bool IsPlaying
        {
            get => _isPlaying;
            set
            {
                _isPlaying = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsPlaying)));
            }
        }

        //public string Lyrics { get; private set; }

        /// <summary>
        /// 时长
        /// </summary>
        public TimeSpan Duration { get; private set; }

        public string TimeString => $@"{Duration.Minutes:D2}:{Duration.Seconds:D2}";

        public MediaFile(string fileName)
        {
            var fileExtension = new FileInfo(fileName).Extension;
            if (!SupportedFileType.IsSupported(fileExtension)) return;
            FullFileName = fileName;
            UpdatePropertiesFromTag(fileName);
        }

        private void UpdatePropertiesFromTag(string fileName)
        {
            if (string.IsNullOrEmpty(fileName) || !QuickIOFile.Exists(fileName))
                return;            
            using(var mediaFile = TagLib.File.Create(fileName))
            {
                Title = ResolveUnknownProperty(mediaFile.Tag.Title);
                Album = ResolveUnknownProperty(mediaFile.Tag.Album);
                Artists = ResolveListProperty(mediaFile.Tag.Performers);
                Genres = ResolveListProperty(mediaFile.Tag.Genres);
                //Lyrics = mediaFile.Tag.Lyrics;
                Duration = mediaFile.Properties.Duration;
            }
        }

        private string ResolveListProperty(string[] valueList)
        {
            if (valueList.Length < 1) return UnknownString;
            return string.Join("/", valueList);
        }

        private string ResolveUnknownProperty(string value)
        {
            return string.IsNullOrWhiteSpace(value) ? UnknownString : value;
        }
    }
}
